﻿using System;
using System.Collections.Generic;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using HYOpeningCommon.DataEnum;
using HYOpeningCommon.OpeningEX;
using HYOpeningCommon.OpeningProfile;
using HYOpeningCommon.Public;
using HYOpeningCreate.Public;
using Transfer.CommonUnit;

namespace HYOpeningCommon.DbOpening
{
	public class DbEquipRectWallOpening : DbFamilyOpening
	{
		public override bool CreateOpeningFamilyInstance(ViewScene ViewScene, out Element elem3D, out Element elem2D)
		{
			elem2D = null;
			elem3D = null;
			if (ViewScene == null)
			{
				return false;
			}
			Autodesk.Revit.DB.View viewOpening2D = ViewScene.m_viewOpening2D;
			if (viewOpening2D == null)
			{
				return false;
			}
			Document document = viewOpening2D.Document;
			OpeningXData openingXData = new OpeningXData();
			base.GetOpeningXDataBase(ref openingXData);
			Transaction transaction = new Transaction(document, "CreateOpeningFamilyInstance");
			try
			{
				FamilySymbolOperator familySymbolOperator = new FamilySymbolOperator(document, this);
				if (!familySymbolOperator.DoCalculate())
				{
					return false;
				}
				FamilySymbol familySymble = familySymbolOperator.GetFamilySymble();
				transaction.Start();
				Wall wall = document.GetElement(this.m_strHostUniqueId) as Wall;
				Level level = document.GetElement(wall.LevelId) as Level;
				OpeningProfileEquipRect openingProfileEquipRect = this.m_profile as OpeningProfileEquipRect;
				UnifiedModified.ActivateFamilySymbol(familySymble);
				XYZ xyz = new XYZ(openingProfileEquipRect.m_position.X, openingProfileEquipRect.m_position.Y, openingProfileEquipRect.m_position.Z - openingProfileEquipRect.m_dExtendDist);
				FamilyInstance familyInstance = document.Create.NewFamilyInstance(xyz, familySymble, wall, level, (Autodesk.Revit.DB.Structure.StructuralType)0);
				document.Regenerate();
				if (openingProfileEquipRect.m_directionProfile.IsSameDirectionTo(familyInstance.FacingOrientation))
				{
					Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(openingProfileEquipRect.m_directionProfile, openingProfileEquipRect.m_position);
					elem3D = MirrorOperater.MirrorParasiticElement(document, familyInstance, plane, false);
				}
				else
				{
					elem3D = familyInstance;
				}
				double num = openingProfileEquipRect.m_dWidth + openingProfileEquipRect.m_dExtendDist * 2.0;
				double num2 = openingProfileEquipRect.m_dHeight + openingProfileEquipRect.m_dExtendDist * 2.0;
				double dPenetrationThickness = openingProfileEquipRect.m_dPenetrationThickness;
				if (familySymbolOperator.IsNeedSetSize())
				{
					elem3D.GetParameter("宽度").Set(num);
					elem3D.GetParameter("高度").Set(num2);
					elem3D.GetParameter("厚度").Set(dPenetrationThickness);
				}
				openingXData.m_dHeight = num2;
				openingXData.m_dWidth = num;
				openingXData.m_dPenetrationThickness = dPenetrationThickness;
				openingXData.m_eOpeningProfile = 1;
				openingXData.m_dMaxZ = openingProfileEquipRect.m_position.Z + num2;
				openingXData.m_dMinZ = openingProfileEquipRect.m_position.Z;
				openingXData.WriteOpeningXData(elem3D);
				transaction.Commit();
			}
			catch (Exception)
			{
				elem3D = null;
				transaction.RollBack();
				return false;
			}
			return true;
		}

		public override bool CreateOpeningPreviewInstance(out Element elemPreview, PreViewScene preViewScene)
		{
			OpeningProfileEquipRect openingProfileEquipRect = this.m_profile as OpeningProfileEquipRect;
			XYZ position = openingProfileEquipRect.m_position;
			XYZ directionProfile = openingProfileEquipRect.m_directionProfile;
			XYZ xyz = XYZ.BasisZ.CrossProduct(directionProfile);
			Transaction transaction = new Transaction(preViewScene.m_doc, "CreateModelGroup");
			bool result;
			try
			{
				XYZ xyz2 = position + directionProfile * (this.m_dHostWidth / 2.0 + preViewScene.m_offset);
				XYZ xyz3 = xyz2 + (openingProfileEquipRect.m_dExtendDist + openingProfileEquipRect.m_dWidth / 2.0) * xyz;
				XYZ xyz4 = xyz2 + (openingProfileEquipRect.m_dExtendDist + openingProfileEquipRect.m_dWidth / 2.0) * xyz.Negate();
				XYZ item = xyz4 + XYZ.BasisZ * (openingProfileEquipRect.m_dExtendDist + openingProfileEquipRect.m_dHeight);
				XYZ item2 = xyz3 + XYZ.BasisZ * (openingProfileEquipRect.m_dExtendDist + openingProfileEquipRect.m_dHeight);
				List<ElementId> list = new List<ElementId>();
				List<XYZ> list2 = new List<XYZ>();
				list2.Add(xyz3);
				list2.Add(xyz4);
				list2.Add(item);
				list2.Add(item2);
				SketchPlane sketchPlane = null;
				if (!CommonInterface.CreateSketchPlane(preViewScene.m_doc, directionProfile, xyz3, ref sketchPlane) || sketchPlane == null)
				{
					elemPreview = null;
					result = false;
				}
				else if (!CommonInterface.CreateModelLine(list2, preViewScene.m_doc, sketchPlane, ref list, preViewScene.m_modelLineElemId))
				{
					elemPreview = null;
					result = false;
				}
				else
				{
					XYZ xyz5 = position + openingProfileEquipRect.m_directionProfile.Negate() * (this.m_dHostWidth / 2.0 + preViewScene.m_offset);
					XYZ xyz6 = xyz5 + (openingProfileEquipRect.m_dExtendDist + openingProfileEquipRect.m_dWidth / 2.0) * xyz;
					XYZ xyz7 = xyz5 + (openingProfileEquipRect.m_dExtendDist + openingProfileEquipRect.m_dWidth / 2.0) * xyz.Negate();
					XYZ item3 = xyz7 + XYZ.BasisZ * (openingProfileEquipRect.m_dExtendDist + openingProfileEquipRect.m_dHeight);
					XYZ item4 = xyz6 + XYZ.BasisZ * (openingProfileEquipRect.m_dExtendDist + openingProfileEquipRect.m_dHeight);
					list2.Clear();
					list2.Add(xyz6);
					list2.Add(xyz7);
					list2.Add(item3);
					list2.Add(item4);
					sketchPlane = null;
					if (!CommonInterface.CreateSketchPlane(preViewScene.m_doc, directionProfile, xyz6, ref sketchPlane) || sketchPlane == null)
					{
						elemPreview = null;
						result = false;
					}
					else if (!CommonInterface.CreateModelLine(list2, preViewScene.m_doc, sketchPlane, ref list, preViewScene.m_modelLineElemId))
					{
						elemPreview = null;
						result = false;
					}
					else
					{
						transaction.Start();
						elemPreview = preViewScene.m_doc.Create.NewGroup(list);
						transaction.Commit();
						result = true;
					}
				}
			}
			catch (Exception)
			{
				transaction.RollBack();
				elemPreview = null;
				result = false;
			}
			return result;
		}

		public override bool LoadOpeningFamily(Autodesk.Revit.DB.Document doc, ref FamilySymbol familySymbol)
		{
			base.SetFamilyName("墙洞口-设备");
			return base.LoadOpeningFamily(doc, ref familySymbol);
		}

		public override bool IsNeedCreateFamily()
		{
			return false;
		}

		public override bool Equals(object obj)
		{
			return base.Equals(obj);
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override void AddSharedParameters(Element elem)
		{
			Transaction transaction = new Transaction(elem.Document, "AddSharedParameter");
			try
			{
				transaction.Start();
				base.AddSharedParameters(elem);
				Parameter parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningMajor);
				if (parameter != null && StrSpecialityObj.m_strSpecialityObj.GetLength(0) > (int)this.m_eSpeciality)
				{
					parameter.Set(StrSpecialityObj.m_strSpecialityObj[(int)this.m_eSpeciality]);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningSize);
				if (parameter != null)
				{
					string text = this.GetOpeningSize(false);
					if (this.m_eCutObject == ECutObject.eHydrant)
					{
						text = "暗装消火栓" + text;
					}
					else if (this.m_eCutObject == ECutObject.eDistributionBox)
					{
						text = "暗装配电箱" + text;
					}
					else if (this.m_eCutObject == ECutObject.eWaterCollector)
					{
						text = "分集水器" + text;
					}
					parameter.Set(text);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningElevTop);
				if (parameter != null)
				{
					string dimLabelItem = this.GetDimLabelItem(EDimItemType.eTop);
					parameter.Set(dimLabelItem);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningElevCenter);
				if (parameter != null)
				{
					string dimLabelItem2 = this.GetDimLabelItem(EDimItemType.eMid);
					parameter.Set(dimLabelItem2);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningElevBottom);
				if (parameter != null)
				{
					string dimLabelItem3 = this.GetDimLabelItem(EDimItemType.eBottom);
					parameter.Set(dimLabelItem3);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningElev);
				if (parameter != null)
				{
					parameter.Set("");
				}
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}

		public override string GetDimLabelItem(EDimItemType eDimItemTyppe)
		{
			OpeningProfileEquipRect openingProfileEquipRect = this.m_profile as OpeningProfileEquipRect;
			if (openingProfileEquipRect == null)
			{
				return null;
			}
			double val = openingProfileEquipRect.m_dHeight / 2.0 + openingProfileEquipRect.m_dExtendDist;
			string result = string.Empty;
			double num = AssistFunc.feetToMM(openingProfileEquipRect.m_position.Z - openingProfileEquipRect.m_dExtendDist) / 1000.0;
			double num2 = num + AssistFunc.feetToMM(val) / 1000.0;
			double num3 = num2 + AssistFunc.feetToMM(val) / 1000.0;
			base.TransformElev2Relative(ref num2, ref num3, ref num);
			switch (eDimItemTyppe)
			{
			case EDimItemType.eBottom:
				result = "底部标高" + num.ToString("f3");
				break;
			case EDimItemType.eMid:
				result = "中心标高" + num2.ToString("f3");
				break;
			case EDimItemType.eTop:
				result = "顶部标高" + num3.ToString("f3");
				break;
			}
			return result;
		}

		public override bool Export2XmlNode(XmlElement xmlNode)
		{
			bool result;
			try
			{
				if (!base.Export2XmlNode(xmlNode))
				{
					result = false;
				}
				else
				{
					result = true;
				}
			}
			catch
			{
				xmlNode = null;
				result = false;
			}
			return result;
		}

		public override bool ImportByXmlNode(XmlNode xmlNode)
		{
			try
			{
				if (!base.ImportByXmlNode(xmlNode))
				{
					return false;
				}
			}
			catch
			{
				return false;
			}
			return true;
		}
	}
}
